package net.zjitc.controller;


import net.zjitc.pojo.Goods;
import net.zjitc.pojo.GoodsAlbum;
import net.zjitc.pojo.Pages;
import net.zjitc.service.GoodsService;
import net.zjitc.utils.APIResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:xuying
 * @date:2024/12/29 12:35:05
 */

@RestController
@RequestMapping("admin/goods")
public class CoodsController {
    @Autowired
    private GoodsService  goodsService;
    @Autowired
    private GoodsAlbumController goodsAlbumController;
    // 查找分类列表
    @GetMapping("/list")
    public APIResponse<Map<String,Object>> getGoodsList(@RequestParam("page") Integer page,
                                                        @RequestParam("pagesize") Integer pagesize){

        Pages pages = new Pages();
        pages.setPage(page);
        pages.setPagesize(pagesize);
        return APIResponse.suc("查询成功",goodsService.getGoodsList(pages));
    }
    // 添加分类
    @PostMapping("/add")
    public APIResponse<Integer> addGood(@RequestBody Goods good){
        return APIResponse.suc("添加成功",goodsService.addGoods(good));
    }
    // 删除分类
    @PostMapping("/del")
    public APIResponse deleteGood(@RequestBody Goods good){
        goodsService.delGoods(good.getId());
        return APIResponse.suc("删除成功",null);
    }

    // 修改分类
    @PostMapping("/save")
    public APIResponse<Goods> updateGood(@RequestBody Goods good){
        if(good.getId()==null)
            return APIResponse.err("修改失败","id 不为空");

        goodsService.updateGoods(good);
        return APIResponse.suc("修改成功",good);
    }

    @GetMapping()
    public APIResponse<Map<String,Object>> findGoodsById(@RequestParam("id") Integer id){
        Goods good = goodsService.findGoodsById(id);
        List<GoodsAlbum> albumList=goodsAlbumController.getGoodsAlbumListById(id).getData();
        Map<String,Object> map = new HashMap<>();
        map.put("good",good);
        map.put("albumList",albumList);
        return APIResponse.suc("查询成功",map);
    }


}
